package com.android.lovely.data

import android.os.Parcelable
import com.android.lovely.room.BotCacheService
import com.android.lovely.room.BotCacheService.convertToUniqueId
import com.android.lovely.room.CacheProfile
import com.android.lovely.room.ProfileDatabase
import com.facebook.ProfileManager
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize

@JsonClass(generateAdapter = true)
data class HomeListData(
    @Json(name = "avatar") val avatar: Map<String, String> = emptyMap(),
    @Json(name = "avatar_detail") val avatarDetail: String = "",
    @Json(name = "background_img") val backgroundImg: String = "",
    @Json(name = "card_num") val cardNum: Int = 0,
    @Json(name = "collect_num") val collectNum: Int = 0,
    @Json(name = "cover_image") val coverImage: Map<String, String> = emptyMap(),
    @Json(name = "first_message") val firstMessage: String = "",
    @Json(name = "gender") val gender: Int = 0,
    @Json(name = "id") val id: Int = 0,
    @Json(name = "intimacy_value") val intimacyValue: Int = 0,
    @Json(name = "is_collect") var isCollect: Boolean = false,
    @Json(name = "is_vip") val isVip: Int = 0,
    @Json(name = "name") val name: String = "",
    @Json(name = "personality") val personality: String = "",
    @Json(name = "point") val point: Int = 0,
    @Json(name = "role_id") val roleId: Int = 0,
    @Json(name = "scenario") val scenario: String = "",
    @Json(name = "show_id") val showId: String = "",
    @Json(name = "tag_list") val roleTag: List<Map<String, String>> = emptyList(),
    @Json(name = "type") val type: Int = 0,
    @Json(name = "user_id") val userId: Int = 0,
    @Json(name = "user_info") val userInfo: Map<String, String> = emptyMap()
) {
    private fun asProfile(): CacheProfile {

        return CacheProfile(
            id.convertToUniqueId(),
            avatar["url"].orEmpty(),
            id,
            intimacyValue,
            false,
            scenario,
            name,
            cardNum,
            personality,
            point,
            gender,
            if (isCollect) 1 else 0,
            backgroundImg,
            userName = userInfo["name"].orEmpty(),
            userId = userInfo["id"]?.toInt() ?: 0,
            roleTag = roleTag.joinToString { it["tag_name"].orEmpty() }
        )
    }

    suspend fun insertOrUpdateProfile() {
        if (BotCacheService.doesBotExist(id).not()) {
            ProfileDatabase.dao.insert(asProfile())
        } else {
            BotCacheService.updateBot(id) {
                avatar = this@HomeListData.avatar["url"].orEmpty()
                intimacyValue = this@HomeListData.intimacyValue
                scenario = this@HomeListData.scenario
                name = this@HomeListData.name
                cardNum = this@HomeListData.cardNum
                personality = this@HomeListData.personality
                point = this@HomeListData.point
                gender = this@HomeListData.gender
                isCollect = if (this@HomeListData.isCollect) 1 else 0
                background = this@HomeListData.backgroundImg
                userName = this@HomeListData.userInfo["name"].orEmpty()
                userId = this@HomeListData.userInfo["id"]?.toInt() ?: 0
                roleTag = this@HomeListData.roleTag.joinToString { it["tag_name"].orEmpty() }
            }
        }
    }
}

@JsonClass(generateAdapter = true)
data class Bot(
    @Json(name = "avatar") val avatar: Map<String, String> = emptyMap(),
    @Json(name = "background_img") val backgroundImg: String = "",
    @Json(name = "can_get_image") val canGetImage: Boolean = false,
    @Json(name = "id") val id: Int = 0,
    @Json(name = "intimacy_value") val intimacyValue: Int = 0,
    @Json(name = "is_collect") val isCollect: Int = 0,
    @Json(name = "card_num") val cardNum: Int = 0,
    @Json(name = "name") val name: String = "",
    @Json(name = "personality") val personality: String = "",
    @Json(name = "point") val point: Int = 0,
    @Json(name = "scenario") val scenario: String = "",
    @Json(name = "gender") val gender: Int = 0,
    @Json(name = "user_info") val userInfo: Map<String, String> = emptyMap(),
    @Json(name = "tag_list") val roleTag: List<Map<String, String>> = emptyList(),
    @Json(name = "chat_name") val chatName: String = "",
    @Json(name = "chat_gender") val chatGender: Int = 1,
    @Json(name = "chat_dec") val chatDec: String = "",
    @Json(name = "chat_model") val chatMode: Map<String, Int>? = null,
)

@Parcelize
@JsonClass(generateAdapter = true)
data class CardItem(
    @Json(name = "card_url") val cardUrl: String = "",
    @Json(name = "id") val id: Int = 0,
    @Json(name = "intimacy_value") val intimacyValue: Int = 0,
    @Json(name = "is_unlock") var isUnlock: Boolean = false,
    @Json(name = "point") val point: Int = 0
) : Parcelable


@JsonClass(generateAdapter = true)
data class CardBot(
    @Json(name = "avatar") val avatar: Map<String, String> = emptyMap(),
    @Json(name = "id") val id: Int = 0,
    @Json(name = "intimacy_value") val intimacyValue: Int = 0,
    @Json(name = "name") val name: String = "",
    @Json(name = "all_card_num") val allCardNum: Int = 0,
    @Json(name = "personality") val personality: String = "",
    @Json(name = "point") val point: Int = 0,
    @Json(name = "gender") val gender: Int = 0,
    @Json(name = "is_collect") val isCollect: Int = 0,
    @Json(name = "background_img") val background: String = "",
    @Json(name = "unlock_all_need_point") val unlockAll: Int = 0,
    @Json(name = "unlock_card_num") val unlocked: Int = 0,
    @Json(name = "user_info") val userInfo: Map<String, String> = emptyMap(),
    @Json(name = "tag_list") val roleTag: List<Map<String, String>> = emptyList(),
)